package com.guanyong.webapi.controllers.page;

import com.guanyong.model.FilmInfoModel;
import com.guanyong.model.FilmModel;
import com.guanyong.model.VideoScoreModel;
import com.guanyong.model.VideoSourceModel;
import com.guanyong.services.CommentService;
import com.guanyong.services.FilmServices;
import com.guanyong.services.VideoScoreService;
import com.guanyong.services.VideoSourceService;
import com.guanyong.webapi.config.CreateVideoURLUtil;
import com.guanyong.webapi.config.VideoLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mxk on 2017/8/25.
 */
@Controller
public class MobileHome extends baseController {


    @Autowired
    FilmServices filmServices;

    @Autowired
    VideoScoreService videoScoreService;

    @Autowired
    VideoSourceService videoSourceService;

    @Autowired
    CreateVideoURLUtil createVideoURLUtil;






    @RequestMapping({ "/index"})
    String index(HttpServletRequest request) {


        return "/home/mobile/index";
    }

    @RequestMapping({"/mobilesearch"})
    String mobilesearch(HttpServletRequest request) {


        return "/home/mobile/search";
    }

    @RequestMapping({  "/movelist"})
    String MoveList(HttpServletRequest request) {


        return "/home/mobile/move";
    }
    @RequestMapping({  "/usercenter"})
    String UserCenter(HttpServletRequest request) {


        return "/home/mobile/usercenter";
    }

    @RequestMapping({  "/video"})
    String Video(Model model, HttpServletRequest request) {
        String filmid=request.getParameter("fid");
        if (filmid!=null && filmid!=""){
            FilmModel filmModel=filmServices.GetFilmModelById(filmid);

            model.addAttribute("film",filmModel);
            VideoScoreModel videoScoreModel=videoScoreService.GetVideoScoreByObjectId(1,filmid);
            model.addAttribute("videoscore",videoScoreModel);
            FilmInfoModel filmInfoModel=filmServices.GetFilmInfoModelById(filmid);

            model.addAttribute("filminfo",filmInfoModel);
            //本地播放
            VideoSourceModel videoSourceModel=videoSourceService.
                    GetVideoScorceByObjectIdType(1,
                            filmModel.getfId(),0);
            if (videoSourceModel!=null){
                Map<String,Object> map = new HashMap<String, Object>();
                String playAuth = null;
                try {
                    if (videoSourceModel.getfSourceurl()!=null){
                        playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }
                if (playAuth!=null){
                    model.addAttribute("videoVoucher",playAuth);
                }else {
                    return "errorPage/500";
                }
            }else{
                return "errorPage/500";
            }
        }
        else{
            return "errorPage/500";
        }



        return "/home/mobile/video";
    }


    @RequestMapping({  "/downapi"})
    public String downLoad(HttpServletRequest request,HttpServletResponse response)
    {
        String path = this.getClass().getResource("/").toString();//获取项目动态绝对路径
        String filePath=path+"static/demo/ApiDemo.java";
        filePath=filePath.replace("file:/","");
        File file = new File(filePath);

        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=ApiDemo.java" );
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            OutputStream os = null; //输出流
            BufferedInputStream bis = null;

            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download ApiDemo" );
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

        return path;

    }
}
